package com.qf.agro.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.pojo.SysUser;
import com.qf.agro.service.sys.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @company：千峰教育
 * @author：zhy
 * @date：2023/2/17
 */
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户管理")
@CrossOrigin
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页查询用户
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @ApiOperation("分页查询用户")
    @ApiImplicitParams(
            value ={
                    @ApiImplicitParam(name = "page", value = "当前页", required = false),
                    @ApiImplicitParam(name = "pageSize", value = "页大小", required = false),
                    @ApiImplicitParam(name = "name", value = "用户名关键字", required = false)
            }
    )
    @GetMapping("/page")
    public ResultInfo findPage(@RequestParam(defaultValue = "1",value = "page") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, String name){
        //1.调用业务层查询
        Page page =  sysUserService.findPage(pageNum,pageSize,name);
        //2.返回
        return ResultInfo.success(page);
    }

    /**
     * 保存用户
     * @param sysUser
     * @return
     */
    @ApiOperation("保存用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "sysUser",value = "用户对象",required = true)
    )
    @PostMapping
    public ResultInfo save(@RequestBody SysUser sysUser){
        //1.调用业务层保存
      return   sysUserService.save(sysUser);

    }

    /**
     * 更新用户
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "更新操作")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "sysUser",value = "用户对象",required = true)
    )
    @PutMapping
    public ResultInfo update(@RequestBody SysUser sysUser){
        //1.调用业务层更新
        sysUserService.update(sysUser);
        //2.返回
        return ResultInfo.success(null);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除操作")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "用户ID",required = true)
    )
    @DeleteMapping
    public ResultInfo delete(Long id){
        //1.调用业务层删除
        sysUserService.delete(id);
        //2.返回
        return ResultInfo.success(null);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id操作")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "用户ID",required = true)
    )
    @GetMapping
    public ResultInfo findById(Long id){
        //1.调用业务层查询
        SysUser user = sysUserService.findById(id);
        //2.返回
        return ResultInfo.success(user);
    }

    
    /**
     * 根据账号密码登录
     * @param map
     * @return
     */
    @ApiOperation(value = "根据账号密码登录")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "username",value = "username",required = true),
                    @ApiImplicitParam(name = "password",value = "password",required = true)
            }
    )
    @PostMapping("/login")
    public ResultInfo login(@RequestBody Map<String,String> map){
        String username=map.get("username");
        String password=map.get("password");
        return sysUserService.login(username,password);
    }

    @ApiOperation(value = "退出")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "userId",value = "userId",required = true),
            }
    )
    @PostMapping("/logout")
    public ResultInfo logout(@RequestBody Map<String,String> map){
        String username=map.get("userId");
        return ResultInfo.success(null);
    }

    @PostMapping("/checkUsername")
    public ResultInfo checkUsername(@RequestBody Map<String,String> map){
        String username = map.get("username");
        boolean isUsed = sysUserService.checkUsername(username);
        if(!isUsed){
            return ResultInfo.success(null);
        }else {
            return ResultInfo.error(null);
        }
    }
}
